from django.conf.urls.defaults import *

from django.conf import settings
from django.contrib.sites.models import Site
from django.core import serializers
from django.views.generic.detail import DetailView

import json

from connie2_django.connie2 import forms
from connie2_django.connie2.models import ConnieRun, VelocityFields, YearForAveraging

# order by id so that it defaults to BRAN (the first one entered):
def fieldsJSON():
    # Wrap calculation in a function, to get around http://code.djangoproject.com/ticket/13843
    return serializers.serialize('json', VelocityFields.objects.filter(enabled=True).order_by('id'), relations={'depths':{'fields':('depth',)}})
uv_fields_json = fieldsJSON()

years_dict = {}
for year in YearForAveraging.objects.all():
    years_dict[year.year] = year.pk

# The site, for when we need to quote ourselves (hardcoded for now):
site = {'domain':'connie.sf.utas.edu.au'}

larvalPhaseFormset = forms.LarvalPhaseFormset()

# Front page and main templates
urlpatterns = patterns('django.views.generic.simple',
        url(r'^$', 'direct_to_template', {'template': 'main.djhtml',
                                          'extra_context': {'fieldsjson': uv_fields_json,
                                                            'yearsjson': json.dumps(years_dict),
                                                            'formset': larvalPhaseFormset,
                                                            'cmaps': settings.COLOUR_MAPS,
                                                            'DEBUG': settings.DEBUG}},
            name='frontpage'),

        url(r'^quickhelp/$', 'direct_to_template', {'template': 'quickhelp.djhtml',
                                                    'extra_context': {'site':site}}, name='quickhelp'),
        url(r'^background/$', 'direct_to_template',
            {'template': 'background.djhtml'}, name='background'),
        url(r'^publications/$', 'direct_to_template', {'template': 'publications.djhtml'}, name='publications'),
        url(r'^noscript/$', 'direct_to_template', {'template': 'noscript.djhtml'}, name="noscript"),
        url(r'^contact/$', 'direct_to_template', {'template': 'contact.djhtml'}, name="contact")
)

# The main dynamic views
urlpatterns += patterns('connie2_django.connie2.views',
    (r'^configurations/$', 'handle_configurations'),
    (r'^configurations/(?P<pk>\d+)/$', DetailView.as_view(template_name='configuration.json',
                                                          queryset=ConnieRun.objects.all()) ),
    url(r'^process/(?P<runid>\d+)/finalpos_result.png', 'perform_run', {'plottype':'final'}, name='finalposplot'),
    url(r'^process/(?P<runid>\d+)/allstats_result.png', 'perform_run', {'plottype':'all'}, name='allstatsplot'),
    url(r'^process/(?P<runid>\d+)/finalstats_result.csv', 'download_csv_results', {'plottype':'final'}, name='finalstatscsv'),
    url(r'^process/(?P<runid>\d+)/allstats_result.csv', 'download_csv_results', {'plottype':'all'}, name='allstatscsv'),
    url(r'^process/(?P<runid>\d+)/finalstats_result.kmz', 'download_kml_results', {'plottype':'final'}, name='finalstatskml'),
    url(r'^process/(?P<runid>\d+)/allstats_result.kmz', 'download_kml_results', {'plottype':'all'}, name='allstatskml')
)
urlpatterns += patterns('connie2_django.connie2.plotter',
    url(r'^colourbar.png', 'colourbar', name='colourbar'),
)

# Admin
from django.contrib.gis import admin
# hep016: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#hooking-adminsite-instances-into-your-urlconf
# "There is really no need to use autodiscover when using your own
# AdminSite instance since you will likely be importing all the
# per-app admin.py modules in your myproject.admin module."
admin.autodiscover()
urlpatterns += patterns('',
    (r'^admin/', include(admin.site.urls)),
)
